﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;

namespace lcs.Web.Admin.demandmaterialinfo
{
    public partial class Addxy :BasePage
    {
        public string type = "";
        public string mtid = "";
        public string sid = "";
        public string depid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["mtid"] != null && Request.QueryString["mtid"].ToString() != "" && Request.QueryString["sid"] != null && Request.QueryString["sid"].ToString() != "")
                {
                    mtid = Request.QueryString["mtid"].ToString();
                    sid = Request.QueryString["sid"].ToString();
                    depid = getdepid().ToString();
                }
                Page.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "" && Request.QueryString["type"] != null && Request.QueryString["type"].ToString() != "")
            {
                int dpid = 0;
                int.TryParse(Request.QueryString["id"].ToString(), out dpid);
                type = Request.QueryString["type"].ToString();
                string strErr = "";
                if (this.hfmbm.Value.Trim().Length == 0)
                {
                    strErr += "物料编码不能为空！\\n";
                }                
                if (!PageValidate.IsDateTime(txtdeliverytime.Text))
                {
                    strErr += "交货时间格式错误！\\n";
                }
                if (this.txtdeliveryadd.Text.Trim().Length == 0)
                {
                    strErr += "交货地点不能为空！\\n";
                }
                if (strErr != "")
                {
                    MessageBox.Show(this, strErr);
                    return;
                }
                string mbm = this.hfmbm.Value;
                string vtype = this.txtvtype.Text;
                decimal num = decimal.Parse(this.txtnum.Text);
                DateTime deliverytime = DateTime.Parse(this.txtdeliverytime.Text);
                string deliveryadd = this.txtdeliveryadd.Text;
                decimal estimate = 0;
                decimal price = 0;
                
                    price = decimal.Parse(hfprice.Value);
                    estimate = price * num / 10000;
                    //string ssextimate = txtestimate.Text;
                    //estimate = decimal.Parse(txtestimate.Text);//要求可以自己修改
                    int depid = getdepid();
                    mtid = Request.QueryString["mtid"].ToString();
                    sid = Request.QueryString["sid"].ToString();
                    int mtids = 0, sids = 0;
                    int.TryParse(mtid, out mtids);
                    int.TryParse(sid, out sids);
                    BLL.protocolquotainfo pb = new BLL.protocolquotainfo();
                    decimal aq = pb.Getquota(depid, mtids, sids);
                    BLL.demandmaterialinfo db = new BLL.demandmaterialinfo();
                    decimal iq = db.Getquota(depid, mtids, sids);
                    if (aq - iq >= price * num)
                    {
                        lcs.Model.demandmaterialinfo model = new lcs.Model.demandmaterialinfo();
                        model.mbm = mbm;
                        model.vtype = vtype;
                        model.num = num;
                        model.deliverytime = deliverytime;
                        model.deliveryadd = deliveryadd;
                        model.estimate = estimate;
                        model.price = price;
                        model.dpid = dpid;
                        lcs.BLL.demandmaterialinfo bll = new lcs.BLL.demandmaterialinfo();
                        if (bll.Add(model) > 0)
                            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "List.aspx?id=" + dpid + "&type=" + type);
                        else
                            Maticsoft.Common.MessageBox.Show(this, "增加失败，同一物料不能增加多个！");
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this, "增加失败，配额不足！");

                    }
            }
        }
        public void btnCancle_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "" && Request.QueryString["type"] != null && Request.QueryString["type"].ToString() != "")
            {
                int dpid = 0;
                int.TryParse(Request.QueryString["id"].ToString(), out dpid);
                type = Request.QueryString["type"].ToString();
                Response.Redirect("List.aspx?id=" + dpid + "&type=" + type);
            }
        }
    }
}